package com.saer.client.grid;

import java.util.ArrayList;

import com.smartgwt.client.types.AutoFitWidthApproach;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public abstract class AutoFitListGrid extends ListGrid {
	
	public AutoFitListGrid() {
		//setCanDragSelectText(true);
	}

	protected void autoFitAllFields() {
		for (ListGridField lgf : getFields()) {
			lgf.setAutoFitWidthApproach(AutoFitWidthApproach.BOTH);
			lgf.setAutoFitWidth(true);
		}
	}

	protected void autoFitFields(String... fields) {
		for (String field : fields) {
			ListGridField lgf = getField(field);
			lgf.setAutoFitWidthApproach(AutoFitWidthApproach.BOTH);
			lgf.setAutoFitWidth(true);
		}
	}

	protected void autoFitAllFields(AutoFitWidthApproach autoFitWidthApproach, String... exculdefields) {
		ArrayList<String> skipFields = new ArrayList<String>();

		for (String field : exculdefields) {
			skipFields.add(field);
		}

		for (ListGridField lgf : getFields()) {
			if (lgf.getName() != null && !skipFields.contains(lgf.getName())) {
				lgf.setAutoFitWidthApproach(autoFitWidthApproach);
				lgf.setAutoFitWidth(true);
			}
		}
	}
	
	protected void autoFitAllFields(String... exculdefields) {
		autoFitAllFields(AutoFitWidthApproach.BOTH, exculdefields);
	}
	
	public void clearData() {
		this.setData(new ListGridRecord[0]);
	}

}
